package com.velvetantler.weather.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.velvetantler.weather.R
import com.velvetantler.weather.bean.CityStar

class CityStarListAdapter(val data: List<CityStar>) :
    RecyclerView.Adapter<CityStarListAdapter.ViewHolder>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(R.layout.item_star_city, parent, false)
        )
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(
        holder: ViewHolder,
        position: Int
    ) {
        val item = data[position]
        holder.view.apply {
            findViewById<TextView>(R.id.tv_city_name).text = item.cityName
            findViewById<TextView>(R.id.tv_city_temperature).text =
                "${item.minTemp}° / ${item.maxTemp}°"
            findViewById<ImageView>(R.id.iv_city_star).setImageResource(R.mipmap.icon_stared)
            setOnClickListener {
                listener?.onClick(item)
            }
        }
    }

    override fun getItemCount(): Int {
        return data.size
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val view = itemView.findViewById<View>(R.id.item_star_city)
    }

    interface OnClickListener {
        fun onClick(position: CityStar)
    }
    private var listener: OnClickListener? = null
    fun setOnClickListener(listener: OnClickListener) {
        this.listener = listener
    }

}